package com.centriole.service.workflow.util;

import java.io.*;
import java.util.Properties;

/**
 * Project: xyc
 * Create by: 刘锐
 * Phone:  18782973813
 * Email：rui.liu@hirebigdata.cn
 * Date：     2015/12/14
 * Description：
 * <p/>
 * Function：
 * 1.
 */
public class Reader {
    public final static String UTF8 = "utf-8";
    public final static String GB2312 = "GB2312";

    /**
     * 读取资源
     * @param resource 资源包下的资源路径
     * @return
     */
    public static InputStream getResourcesInputStream(String resource) {
        return Reader.class.getClassLoader().getResourceAsStream(resource);
    }

    /**
     * 读取Properties配置
     * @param property Properties在资源包下的路径
     * @param encoding 读取使用编码
     * @return
     */
    public static Properties readProperties(String property, String encoding) {
        Properties p = new Properties();
        try {
            InputStream inputStream = getResourcesInputStream(property);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, encoding);
            p.load(inputStreamReader);
            inputStream.close();
            inputStreamReader.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return p;
    }

    /**
     * 获取资源BufferedReader
     * @param resource 资源包下的资源路径
     * @param encoding 读取使用编码
     * @return
     * @throws UnsupportedEncodingException
     */
    public static BufferedReader getReader(String resource, String encoding) throws UnsupportedEncodingException {
        return new BufferedReader(new InputStreamReader(getResourcesInputStream(resource), encoding));
    }

}
